<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="发布者id" prop="uId">-->
<!--        <el-input-->
<!--          v-model="queryParams.uId"-->
<!--          placeholder="请输入发布者id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="培训名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入培训名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择培训开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="overTime">
        <el-date-picker clearable
          v-model="queryParams.overTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择培训结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="trainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="index" />
      <el-table-column label="发布者" align="center" prop="createBy" />
      <el-table-column label="培训名称" align="center" prop="name" />
      <el-table-column label="培训文件" align="center" prop="filePath" >
        <template slot-scope="scope">
          <el-link type="primary" :href="baseUrl + scope.row.filePath" target="_blank"><i
            class="el-icon-view el-icon--left"></i>查看
          </el-link>
        </template>
      </el-table-column>

      <el-table-column label="介绍" align="center" prop="introduce" />
      <el-table-column label="培训开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="培训结束时间" align="center" prop="overTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.overTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="insertTrain(scope.row)"

          >申请</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listTrain, getTrain, delTrain, addTrain, updateTrain } from "@/api/train/train";
import { listRecord, getRecord, delRecord, addRecord, updateRecord,apply } from "@/api/train/record";
export default {
  name: "Train",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训表格数据
      trainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uId: null,
        name: null,
        filePath: null,
        introduce: null,
        startTime: null,
        overTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询培训列表 */
    getList() {
      this.loading = true;
      listTrain(this.queryParams).then(response => {
        this.trainList = response.rows;
        // 使用 Array.map() 添加索引序号
        this.trainList = this.trainList.map((record, index) => {
          // 添加名为 "index" 的属性，其值为索引序号
          return { ...record, index: index + 1 };
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uId: null,
        name: null,
        filePath: null,
        introduce: null,
        startTime: null,
        overTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },


    insertTrain(row) {
      const ids = row.id || this.ids;
      this.form.tId = ids;
      this.$modal.confirm('是否确认申请培训编号为"' + ids + '"的培训？').then(() => {
        console.log("点击-------")
        return apply(this.form).then(response => {
          this.$modal.msgSuccess("申请成功");
          this.open = false;
          this.getList();
        });
      }).catch(() => {
        // 用户点击了取消按钮，可以添加一些逻辑或者不做任何事情
      });
    }



  }
};
</script>
